<?php

namespace Home\Controller;

use Home\Core\CommandSDK;

class UserGroupController extends BaseController
{
    /**
     * 检索组织列表
     */
    public function user_group()
    {
        $param = I('get.');
        $param['parentid']= 0 ;
        $cookie = base64_decode($_COOKIE['user_info']);
        parse_str($cookie);
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::OrganizationList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $this->assign('companyname', $companyname);
        $this->assign('array', $array);
        $this->display();
    }

    /**
     * 返回json数据
     */
    public function user_group_json()
    {
        $param = I('get.');
        if(empty($param['parentid'])){
            $param['parentid']= 0 ;
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::OrganizationList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 增加组织
     */
    public function addOrganization_post()
    {
        $param = I('post.');
        $param['parentid'] = $param['parentid'] ? $param['parentid'] : 0;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::AddOrganization, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);

    }

    /**
     * 修改组织
     */
    public function modifyOrganization_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ModifyOrganization, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 删除组织
     */
    public function delOrganization_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::DelOrganization, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 获取组织用户列表
     */
    public function UserList_post()
    {
        $param = I('post.');
        if(empty($param['pageid'])){
            $param['pageid'] = '1';
        }
        if(empty($param['pagecount'])){
            $param['pagecount'] = '10000';
        }
         if($_SESSION['AreaId'] !== null){
            $param['areaid']=$_SESSION['AreaId'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetClientList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $array['page'] = [    // 考虑到用到分页的地方
            'totnum'=>$array['totnum'],   // 总条数
            'pageid'=>$param['pageid'],   // 当前页数
            'pagecount'=>$param['pagecount'],   // 显示条数
        ];
        $this->ajaxReturn($array);
    }

    /**
     * 删除用户
     */
    public function delUser_post()
    {
        $ids = $_POST['ids'];
        $success = [];   // 准备删除成功的数组
        $error = [];   // 准备删除失败的数组
        foreach($ids as $value){
            $param['accountiddst'] = $value;
            $sdk = new CommandSDK();
            $return = $this->http_request($sdk::DelUser, $param, 'GET');
            $result = $return->result;  //获取result的值
            if(!$result){   // 删除成功，记录用户ID
                array_push($success,$value);
                continue;
            }
            // 删除失败的ID数组
            array_push($error,$value);
        }
        $data = ['result'=>0,'msg'=>'删除成功！','ids'=>$success];
        if(!empty($error)){  // 删除失败
            $data = ['result'=>1,'msg'=>'部分用户删除失败！','ids'=>$success];
        }
        $this->ajaxReturn($data);
    }

    /**
     * 撤销组织用户
     */
    public function undoOrganizationUser()
    {
            $param['departmentid']=I('post.departmentid');
            $param['accountiddst']=I('post.accountiddst');
            $sdk = new CommandSDK();
            $return = $this->http_request($sdk::undoOrganizationUser, $param, 'GET');
            $array = json_decode(json_encode($return), true);
            $array['msg']=$sdk::error_code[$return->result];
            $this->ajaxReturn($array);
    }

}